﻿using Sundry.BLL;
using Sundry.IBLL;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Sundry.Model;

namespace Sundry.App_Start.Common
{
    public class PubliFunction
    {
        static IServiceEntrace _serviceEntrace = new ServiceEntrace();

        #region 获得文件物理路径
        /// <summary>
        /// 获得文件物理路径
        /// </summary>
        /// <param name="path">需要获取的物理路径</param>
        /// <returns></returns>
        public static string GetMapPath(string path)
        {
            if (System.Web.HttpContext.Current != null)
            {
                return System.Web.HttpContext.Current.Server.MapPath(path);
            }
            else
            {
                return System.Web.Hosting.HostingEnvironment.MapPath(path);
            }
        }
        #endregion

        #region 判断图片是否存在
        /// <summary>
        /// 判断后台图片是否存在，不存在则返回默认图片
        /// </summary>
        /// <param name="strImgPath"></param>
        /// <returns></returns>
        public static string ExistsImg(string strImgPath)
        {
            string strValue = "/Content/Admin/images/luosuo.jpg";
            if (!string.IsNullOrEmpty(strImgPath))
            {
                if (!strImgPath.StartsWith("http") && !strImgPath.StartsWith("www."))
                {
                    if (File.Exists(HttpContext.Current.Server.MapPath(strImgPath)))
                    {
                        strValue = strImgPath;
                    }
                }
                else
                {
                    strValue = strImgPath;
                }
            }
            return strValue;
        }
        #endregion

        #region 添加管理员操作日志
        /// <summary>
        /// 添加管理员操作日志
        /// </summary>
        /// <param name="action">动作类型（1：增，2：修改，3：查看，4：删除）</param>
        /// <param name="title">操作标题</param>
        /// <param name="content">操作内容</param>
        /// <returns></returns>
        public static bool InsertOperation(int action, string title, string content)
        {
            Sundry.Model.AdminOperation model = new Model.AdminOperation()
            {
                Action = action,
                AdminId = int.Parse(UserCookie.GetAdminId()),
                AuthoryId = int.Parse(UserCookie.GetAdminAuthoryId()),
                Content = content,
                CreateTime = DateTime.Now,
                Title = title,
                OperateIP = IpSearch.GetRealIP(),
                OperateAddress = IpSearch.GetAdrByIp()
            };

            return _serviceEntrace.GetAdminOperationService.AddEntity(model) > 0 ? true : false;
        }
        #endregion

        #region 获取菜单列表
        /// <summary>
        /// 获取总菜单列表管理员
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<AdminMenu> GetAdminMenu()
        {
            return _serviceEntrace.GetAdminMenuService.GetAdminMenu(
                m => m.IsAdmin == 1 && m.IsShow == 1,
                m => m.ParentMenuId, m => m.AdminOrderNum,
                m => new 
                {
                    AdminMenuId = m.AdminMenuId,
                    Icon = m.Icon,
                    MenuActionUrl = m.MenuActionUrl,
                    MenuLevel = m.MenuLevel,
                    MenuName = m.MenuName,
                    ParentMenuId = m.ParentMenuId
                }
             ).ToList().Select(m => new AdminMenu
             {
                 AdminMenuId = m.AdminMenuId,
                 Icon = m.Icon,
                 MenuActionUrl = m.MenuActionUrl,
                 MenuLevel = m.MenuLevel,
                 MenuName = m.MenuName,
                 ParentMenuId = m.ParentMenuId
             });
        }
        #endregion
    }
}